<?php
/**
 * 园所管理员model
 */
class Nursery_admin_model extends CT_Model
{
	public $_table = 'nursery_admin';
	public $_table_role = 'nursery_role';
	
	public function __construct()
	{
		parent::__construct();
		$this->load->database();
	}

	//判断管理员名称是否重复
	public  function  check_admin_name($admin_name,$admin_id=NULL){

		if(empty($admin_id)){
			$this->db->where('username',$admin_name);
		}else{
			$this->db->where('id <>',$admin_id);
			$this->db->where('username',$admin_name);
		}

		$data = $this->db->select('id')->get($this->_table)->row_array();
		return $data['id'];

	}
	
	//判断管理员昵称是否已存在
	public  function  check_admin_nickname($admin_name,$admin_id=NULL)
	{
	
	    if(empty($admin_id)){
	        $this->db->where('nickname',$admin_name);
	    }else{
	        $this->db->where('id <>',$admin_id);
	        $this->db->where('nickname',$admin_name);
	    }
	
	    $data = $this->db->select('id')->get($this->_table)->row_array();
	    return $data['id'];
	
	}
	
	/**
	 * 获取管理员列表
	 * @param array $condition
	 * @param int $offset
	 * @param int $limit
	 */
	public  function get_list($condition=array(),$offset=0,$limit=20)
	{
	
	    $sql='SELECT a.id,a.username,a.status,a.nickname,r.name as role_name FROM '.$this->_table.' as a ';
	    $sql.='LEFT JOIN '.$this->_table_role.' as r ON a.rid=r.id ';
	    $sql.='LIMIT '.$offset.','. $limit;
	    return $this->db->query($sql)->result_array();
	
	}
	
}